package com.zdpx.pxframework.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdpx.pxframework.core.validator.group.AddGroup;
import com.zdpx.pxframework.core.validator.group.UpdateGroup;
import com.zdpx.pxframework.core.vo.PageResult;
import com.zdpx.pxframework.core.vo.Result;
import com.zdpx.pxframework.system.api.RoleInfoControllerApi;
import com.zdpx.pxframework.system.api.entity.RoleInfo;
import com.zdpx.pxframework.system.service.RoleInfoService;
import com.zdpx.pxframework.system.vo.query.RoleInfoQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


/**
 * 角色 接口控制器
 *
 * @Description:
 * @Author: Cheng XiaoXiao
 * @Date: 2022-02-27 15:20:14
 */
@RestController
@RequestMapping("/role-info")
public class RoleInfoController implements RoleInfoControllerApi {
    @Resource
    private RoleInfoService roleInfoService;


    /**
     * 获取条件查询信息
     *
     * @param roleInfoQuery 查询条件
     * @return 查询封装
     */
    private LambdaQueryWrapper<RoleInfo> getRoleInfoQueryWrapper(RoleInfoQuery roleInfoQuery) {
        LambdaQueryWrapper<RoleInfo> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleInfoQuery.getRoleName())) {
            wrapper.like(RoleInfo::getRoleName, roleInfoQuery.getRoleName());
        }
        if (!StringUtils.isEmpty(roleInfoQuery.getRoleKey())) {
            wrapper.like(RoleInfo::getRoleKey, roleInfoQuery.getRoleKey());
        }
        if (!StringUtils.isEmpty(roleInfoQuery.getRemark())) {
            wrapper.like(RoleInfo::getRemark, roleInfoQuery.getRemark());
        }
        return wrapper;
    }

    /**
     * 按照条件查询角色信息
     *
     * @param roleInfoQuery 查询条件
     * @return 条件结果信息
     */
    @Override
    @GetMapping
    public Result<List<RoleInfo>> list(RoleInfoQuery roleInfoQuery) {
        return Result.success(roleInfoService.list(getRoleInfoQueryWrapper(roleInfoQuery)));
    }

    /**
     * 分页及条件查询角色信息
     *
     * @param page          页码
     * @param size          分页大小
     * @param roleInfoQuery 查询条件
     * @return 分页结果信息
     */
    @Override
    @GetMapping("/{size}/{page}")
    public Result<PageResult<RoleInfo>> search(@PathVariable("size") Long size, @PathVariable("page") Long page, RoleInfoQuery roleInfoQuery) {
        if (page <= 0) {
            page = 1L;
        }
        if (size <= 0) {
            size = 10L;
        }
        Page<RoleInfo> pageParam = new Page<>(page, size);

        roleInfoService.page(pageParam, getRoleInfoQueryWrapper(roleInfoQuery));
        PageResult<RoleInfo> pageResult = new PageResult<RoleInfo>(pageParam.getTotal(), pageParam.getSize(), pageParam.getCurrent(), pageParam.getRecords());

        return Result.success(pageResult);
    }

    /**
     * 根据ID获取角色详情
     *
     * @param id 角色ID
     * @return 角色详细信息
     */
    @Override
    @GetMapping("/{id}")
    public Result<RoleInfo> detail(@PathVariable("id") String id) {
        return Result.success(roleInfoService.detailById(id));
    }

    /**
     * 增加角色信息
     *
     * @param roleInfo 角色增加实体
     * @return 增加后的角色信息
     */
    @Override
    @PostMapping
    public Result<RoleInfo> add(@RequestBody @Validated(AddGroup.class) RoleInfo roleInfo) {
        roleInfoService.saveRoleInfo(roleInfo);
        return Result.success(roleInfo);
    }

    /**
     * 修改角色信息
     *
     * @param id       角色ID
     * @param roleInfo 角色修改实体
     * @return 修改后的角色信息
     */
    @Override
    @PutMapping("/{id}")
    public Result<RoleInfo> updateById(@PathVariable("id") String id, @RequestBody @Validated(UpdateGroup.class) RoleInfo roleInfo) {
        roleInfo.setId(id);
        roleInfoService.updateRoleInfoById(roleInfo);
        return Result.success(roleInfoService.getById(id));
    }


    /**
     * 删除角色信息
     *
     * @param id 角色ID
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") String id) {
        roleInfoService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 删除角色信息
     *
     * @param idList 角色ID数组
     * @return 删除结果
     */
    @Override
    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemove(@RequestBody List<String> idList) {
        roleInfoService.removeByIds(idList);
        return Result.success(true);
    }

    /**
     * 根据用户ID获取拥有的角色标识列表
     *
     * @param userId 用户ID
     * @return 角色标识列表
     */
    @Override
    @GetMapping("/role/keys/user/{userId}")
    public Result<Set<String>> getRoleKeysByUserId(@PathVariable("userId") String userId) {
        return Result.success(roleInfoService.getRoleKeysByUserId(userId));
    }

    /**
     * 根据角色标识数组，获取角色列表
     *
     * @param roleKeys 角色标识数组
     * @return 角色列表
     */
    @Override
    @GetMapping("/list")
    public Result<List<RoleInfo>> listByRoleKeys(@RequestParam("roleKeys") List<String> roleKeys) {
        return Result.success(roleInfoService.listRoleInfoByRoleKeys(roleKeys));
    }

}
